package com.ztc.ZtcTransportCapitalLog.service;


import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.ztc.ZtcCapitalIncome.dao.ZtcCapitalIncomeDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.dao.ZtcTransportDao;
import com.ztc.ZtcTransportCapitalLog.bean.ZtcTransportCapitalLog;
import com.ztc.ZtcTransportCapitalLog.dao.ZtcTransportCapitalLogDao;
import com.ztc.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service("ZtcTransportCapitalLog")
public class ZtcTransportCapitalLogManager {

    @Autowired
    ZtcTransportCapitalLogDao ztcTransportCapitalLogDao;
    @Autowired
    ZtcTransportDao ztcTransportDao;
    @Autowired
    ZtcCapitalIncomeDao ztcCapitalIncomeDao;

    public String save(ZtcTransportCapitalLog ztcTransportCapitalLog){
        ztcTransportCapitalLogDao.save(ztcTransportCapitalLog);
        return ztcTransportCapitalLog.getId();
    }

    public ZtcTransportCapitalLog get(String id){
        return ztcTransportCapitalLogDao.get(id);
    }

    public ZtcTransportCapitalLog findUniqueBy(String propertyName, Object value) {
        return ztcTransportCapitalLogDao.findUniqueBy(propertyName, value);
    }

    public List<ZtcTransportCapitalLog> findBy(String propertyName1, Object value1, String propertyName2, Object value2) {
        return ztcTransportCapitalLogDao.findBy(propertyName1, value1, propertyName2, value2);
    }

    public List<ZtcTransportCapitalLog> findBy(String propertyName, Object value) {
        return ztcTransportCapitalLogDao.findBy(propertyName, value);
    }

    /** 获取当前公司收支明细 -时间段搜索 */
    public HashMap<String,Object> searchZtcTransportCapitalLog(String transportId, Integer pageNumber, Integer pageSize, Integer type, String startDate, String endDate,String fullName) {
        ZtcTransport ztcTransport=ztcTransportDao.get(transportId);
        if(ztcTransport==null){
            return JsonWrapper.failureWrapperMsg("未查询到公司相关信息！");
        }
        Pager<HashMap<String,Object>> pager= ztcTransportCapitalLogDao.searchZtcTransportCapitalLog(transportId,type,pageNumber,pageSize,startDate,endDate,fullName);
        List<HashMap<String,Object>> result=new ArrayList<>();
        replaceZtcTransportCapitalLog(pager.getContent(), result);
        return JsonWrapper.successPagerWrapper(result, pager.getRecordTotal());
    }

    /** 获取全部 */
    public List<Object[]> getAllCapitalLog(String transportId,Integer type,String fullName,String startDate,String endDate) {
        return ztcTransportCapitalLogDao.getAllCapitalLog(transportId,type,fullName,startDate,endDate);
    }


    private void replaceZtcTransportCapitalLog(List<HashMap<String,Object>> pager, List<HashMap<String, Object>> result) {
        for (HashMap<String,Object> map:pager) {
            ZtcTransport ztcTransport1=ztcTransportDao.get(map.get("transportId").toString());
            map.put("company",ztcTransport1.getFullName());
            map.replace("type", ExcelUtil.getCapitalLogType(Integer.valueOf(map.get("type").toString())));
            map.remove("transportId");
            result.add(map);
        }
    }

    /**
     * 根据公司ID获取最新N条收益信息.
     * @param tid  公司ID
     * @param number 条数(6)
     * @param type  公司类型
     * @return
     */
    public HashMap<String,Object> getUpCapitalLogList(String tid,Integer number,Integer type){
        HashMap<String,Object> result=new HashMap<>();
        result.put("type",type);
        if(type==0){
            result.put("info",ztcCapitalIncomeDao.getUpCapitalIncome(number));
        }else{
            result.put("info",ztcTransportCapitalLogDao.getUpCapitalLogListByTid(tid,number));
        }
        return JsonWrapper.successWrapper(result);
    }

    /**
     * 根据年份获取该年度每月收益总额.
     * @param tid  公司ID
     * @param year  年份
     * @param type  公司类型
     * @return 每月收益额
     */
    public HashMap<String,Object> getCapitalTotal(String tid,Integer year,Integer type){
        if(type==0){
           return JsonWrapper.successWrapper(ztcCapitalIncomeDao.getCapitalTotal(year));
        }else{
            return JsonWrapper.successWrapper(ztcTransportCapitalLogDao.getCapitalTotalByTid(tid,year));
        }
    }

    /**
     * 保存公司资金交易记录
     *
     * @param orderId      订单ID
     * @param transportId  公司ID
     * @param transportFee 交易金额
     * @param afterBalance 交易后金额
     * @param type         类型
     * @param remark       描述
     */
    public void saveTransportCapitalLog(String orderId, String transportId, BigDecimal transportFee,
                                         BigDecimal afterBalance, String type, String remark) {
        ZtcTransportCapitalLog transportCapitalLog = new ZtcTransportCapitalLog();
        transportCapitalLog.setBusinessId(orderId);
        ZtcTransport transport = new ZtcTransport();
        transport.setId(transportId);
        transportCapitalLog.setZtcTransport(transport);
        transportCapitalLog.setCreateTime(new Date());
        transportCapitalLog.setMoney(transportFee);
        transportCapitalLog.setAfterBalance(afterBalance);
        transportCapitalLog.setType(type);
        transportCapitalLog.setRemark(remark);
        ztcTransportCapitalLogDao.save(transportCapitalLog);
    }
}
